/*
 * Created on May 8, 2004
 */
package dk.aiszone.lang.autosource.generatorobjects;

import dk.aiszone.lang.autosource.generatorobjects.interfaces.GeneratorObject;

/**
 * Representation of an <code>Import</code>.<br>
 * The <code>Import</code> class is used contain and organise <code>Imports</code>.
 * 
 * @author <a href="mailto:ais@mail.dk">Ais</a>
 * @since 1.0
 * @version 1.0
 */
public class Import extends GeneratorObject implements Comparable<Import> {

    private String packageName;

    /**
     * Constructor for the <code>Import</code> class.
     * 
     * @param packageName
     *            The package and name of the <code>class</code> to import.
     */
    public Import(String packageName) {
        this.packageName = packageName;
    }

    public int compareTo(Import other) {
        int val1 = getValue(packageName);
        int val2 = getValue(other.packageName);

        if (val1 > val2) {
            return 1;
        } else if (val1 < val2) {
            return -1;
        }
        return packageName.compareTo(other.packageName);
    }

    /**
     * Calculates and returns the internal order value of the imports
     * 
     * @return Order value
     */
    public int getOrderValue() {
        return getValue(packageName);
    }

    /**
     * Retrieve the package name declared in the <code>Import</code>
     * 
     * @return The package and name of the <code>class</code> to import.
     */
    public String getPackageName() {
        return packageName;
    }

    /**
     * Find the representative value of the import in the following order
     * <ul>
     * <li>1. java</li>
     * <li>2. javax</li>
     * <li>3. org</li>
     * <li>4. com</li>
     * <li>5. net</li>
     * <li>6. everything else</li>
     * </ul>
     * 
     * @param packageName
     *            name of package
     * @return Representative value of package name
     */
    private int getValue(String packageName) {
        if (packageName.startsWith("java.")) {
            return 1;
        } else if (packageName.startsWith("javax.")) {
            return 2;
        } else if (packageName.startsWith("org.")) {
            return 3;
        } else if (packageName.startsWith("com.")) {
            return 4;
        } else if (packageName.startsWith("net.")) {
            return 5;
        } else {
            return 6;
        }
    }
}